package main

import (
	"time"
	"fmt"
)


func changeDemo(){
	//这个channel是接收数据的
	var channels [10]chan<- int
	//var c chan int //c == nil
	for i :=0; i<10; i++{
		channels[i] = createWorker(i)
	}

	for i :=0; i<10; i++{
		channels[i] <- 'a' + i
	}

	time.Sleep(time.Minute)
}

func worker(id int, c chan int)  {
	//go func(){
		//for {
		//	n,ok := <-c
		//	if (!ok){
		//		break;
		//	}
		//	//channel是接收数据的，因此可以在channel取出数据
		//	fmt.Printf("workder =%d, receiver =%c\n", id, n)
		//}
	//}()
	for n:= range c{
		//channel是接收数据的，因此可以在channel取出数据
		fmt.Printf("workder =%d, receiver =%c\n", id, n)
	}
}

func createWorker(id int) chan<- int {
	c := make(chan int)
	go worker(id,c)
	return c
}

func bufferdChannel(){
	c := make(chan int, 2)

	worker(0,c)

	c <-'a'
	c <-'b'
	c <-'c'

	time.Sleep(time.Millisecond)
}

func channelClose()  {
	c := make(chan int, 2)

	worker(0,c)

	c <-'a'
	c <-'b'
	c <-'c'

	close(c)
	time.Sleep(time.Millisecond)
}
func main() {
	//changeDemo()
	//bufferdChannel()
	channelClose()
}
